#include <windows.h>
#include <tchar.h>
#include "resource.h"
#include "DockWindow.h"

HWND hwndDock1;
HWND hwndDock2;

TCHAR szAppName[] = _T("Main Window");

HWND hwndMain;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int x = 200, y = 200;
	static int count = 1;
	TCHAR szName[32];

	switch(msg)
	{
	case WM_CREATE:
		return 0;

	case WM_NCACTIVATE:
		return HANDLE_NCACTIVATE(hwnd, hwnd, wParam, lParam);

	case WM_ENABLE:
		return HANDLE_ENABLE(hwnd, hwnd, wParam, lParam);

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_CLOSE:
		DestroyWindow(hwnd);
		return 0;

	case WM_COMMAND:

		switch(LOWORD(wParam))
		{
		case IDM_HELP_ABOUT:
			MessageBox(hwnd, "Look how all the popups go inactive", szAppName, MB_OK);
			return 0;

		case IDM_FILE_NEW:
			
			wsprintf(szName, "Tool Window %d", count++);
			CreateDockWnd(hwnd, szName, x, y, 400, 64);

			x += 10;
			y += 10;

			if(x > 800 || y > 600)
			{
				x = 0;
				y = 0;
			}
		}

		return 0;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

HWND CreateMainWnd(HWND hwndParent)
{
	HWND hwnd;
	
	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
				szAppName,				// window class name
				szAppName,				// window caption
				WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
				CW_USEDEFAULT,			// initial x position
				CW_USEDEFAULT,			// initial y position
				650,					// initial x size
				320,					// initial y size
				NULL,					// parent window handle
				NULL,					// use window class menu
				GetModuleHandle(0),		// program instance handle
				NULL);					// creation parameters

	return hwnd;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int iShowCmd)
{
	MSG			msg;
	WNDCLASSEX	wc;
	HACCEL		hAccelTable;

	ZeroMemory(&wc, sizeof(wc));

	//Window class for the main application parent window
	wc.cbSize			= sizeof(wc);
	wc.style			= 0;
	wc.lpfnWndProc		= WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInst;
	wc.hIcon			= LoadIcon(0, MAKEINTRESOURCE(IDI_APPLICATION));
	wc.hCursor			= LoadCursor (NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)(COLOR_APPWORKSPACE+1);
	wc.lpszMenuName		= MAKEINTRESOURCE(IDR_MENU1);
	wc.lpszClassName	= szAppName;
	wc.hIconSm			= LoadIcon (NULL, IDI_APPLICATION);

	RegisterClassEx(&wc);

	hwndMain = CreateMainWnd(0);
	ShowWindow(hwndMain, iShowCmd);

	hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCELERATOR1));

	while(GetMessage(&msg, NULL,0,0))
	{
		if(!TranslateAccelerator(hwndMain, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}


	return 0;
}